package com.tbit.api;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.tbit.manager.api.MemberFeeApi;
import com.tbit.manager.api.dto.UserMemberDTO;
import com.tbit.uqbike.object.pojo.UserMember;
import com.tbit.uqbike.service.business.MemberFeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 免押会员费
 *
 * @author ：zhangtianyou
 * @since 2024/11/22
 */
@RestController
@Slf4j
public class MemberFeeApiImpl implements MemberFeeApi {

    @Autowired
    private MemberFeeService memberFeeService;

    @Override
    public List<UserMemberDTO> userMemPage(Integer userId, Integer pageSize, Integer pageNum) {
        List<UserMember> userMemberList = memberFeeService.userMemPage(userId, pageSize, pageNum);
        ArrayList<UserMemberDTO> userMemberDTOList = new ArrayList<>();
        for (UserMember userMember : userMemberList) {
            DateTime expireTime = DateUtil.endOfDay(DateUtil.parse(userMember.getExpireTime()));
            DateTime nowDay = DateUtil.endOfDay(new Date());
            if (expireTime.compareTo(nowDay) >= 0){
                userMember.setIsValid(1);
            }else {
                userMember.setIsValid(0);
            }
            UserMemberDTO userMemberDTO = new UserMemberDTO();
            BeanUtils.copyProperties(userMember, userMemberDTO);
            userMemberDTOList.add(userMemberDTO);
        }
        return userMemberDTOList;
    }
}
